<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <!-- <el-form-item label="用户账号" prop="username">
        <el-input v-model="formData.username" placeholder="请输入用户账号" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="formData.password" placeholder="请输入密码" />
      </el-form-item> -->
      
      <el-divider  content-position="left" class="blue-divider">基本信息</el-divider>
      <el-row>
        <el-col :span="12">
          <el-form-item label="用户昵称" prop="nickname">
            <el-input v-model="formData.nickname" placeholder="请输入用户昵称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号码" prop="mobile">
            <el-input v-model="formData.mobile" placeholder="请输入手机号码" />
          </el-form-item>
        </el-col>
      </el-row>

       <el-row>
        <el-col :span="12">
          <el-form-item label="用户性别" prop="sex">
            <el-select v-model="formData.sex" placeholder="请选择用户性别">
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_USER_SEX)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属园区" prop="schoolCode">
            <el-select v-model="formData.schoolCode" placeholder="请选择所属园区">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.SCHOOL_CODE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

       <el-row>
        <el-col :span="12">
          <el-form-item label="出生日期" prop="birthDate">
            <el-date-picker
              v-model="formData.birthDate"
              type="date"
              value-format="x"
              placeholder="选择出生日期"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="民族" prop="ethnicity">
            <el-select v-model="formData.ethnicity" placeholder="请选择民族">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.SCHOOL_TEACHER_ETHNICITY)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="入职时间" prop="hireDate">
            <el-date-picker
              v-model="formData.hireDate"
              type="date"
              value-format="x"
              placeholder="选择入职时间"
            />
          </el-form-item>

      &nbsp;
      <el-divider  content-position="left" class="blue-divider">其它信息</el-divider>
       <el-row>
        <el-col :span="12">
          <el-form-item label="学历" prop="education">
            <el-select v-model="formData.education" placeholder="请选择学历">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.SCHOOL_TEACHER_EDUCATION)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="技术职称" prop="technicalTitle">
            <el-select v-model="formData.technicalTitle" placeholder="请选择技术职称">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.SCHOOL_TEACHER_TECHNICAL_TITLE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

       <el-row>
        <el-col :span="12">
          <el-form-item label="持证类别" prop="certificateType">
            <el-select v-model="formData.certificateType" placeholder="请选择持证类别">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.SCHOOL_TEACHER_CERTIFICATE_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="政治面貌" prop="politicalStatus">
            <el-select v-model="formData.politicalStatus" placeholder="请选择政治面貌">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.SCHOOL_TEACHER_POLITICAL_STATUS)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      &nbsp;
      <el-divider content-position="left" class="blue-divider">家庭信息</el-divider>
      <el-form-item label="身份证号" prop="idNumber">
        <el-input v-model="formData.idNumber" placeholder="请输入身份证号" />
      </el-form-item>
      <el-form-item label="家庭地址" prop="homeAddress">
        <el-input v-model="formData.homeAddress" type="textarea" placeholder="请输入家庭地址" />
      </el-form-item>

      &nbsp;
      <el-divider  content-position="left" class="blue-divider">其它</el-divider>
      <el-form-item label="就职状态" prop="employmentStatus">
        <el-select v-model="formData.employmentStatus" placeholder="请选择就职状态">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SCHOOL_TEACHER_EMPLOYMENT_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      
      <el-form-item v-if="formData.employmentStatus == 4" label="离职时间" prop="leaveDate">
            <el-date-picker
              v-model="formData.leaveDate"
              type="date"
              value-format="x"
              placeholder="选择离职时间"
            />
      </el-form-item>
      
      <el-form-item v-if="formData.employmentStatus == 4" label="离职原因" prop="leaveReason">
        <el-input v-model="formData.leaveReason" type="textarea" placeholder="请输入离职原因" />
      </el-form-item>

      <el-form-item v-if="formData.employmentStatus == 3" label="退休日期" prop="retirementDate">
        <el-date-picker
          v-model="formData.retirementDate"
          type="date"
          value-format="x"
          placeholder="选择退休日期"
        />
      </el-form-item>
      
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" type="textarea" placeholder="请输入备注" />
      </el-form-item>
      
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { UsersApi, UsersVO } from '@/api/school/teacher'

/** 教师信息 表单 */
defineOptions({ name: 'UsersForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  username: undefined,
  password: undefined,
  nickname: undefined,
  remark: undefined,
  deptId: undefined,
  postIds: undefined,
  email: undefined,
  mobile: undefined,
  sex: undefined,
  avatar: undefined,
  status: undefined,
  loginIp: undefined,
  loginDate: undefined,
  schoolCode: undefined,
  birthDate: undefined,
  ethnicity: undefined,
  education: undefined,
  technicalTitle: undefined,
  certificateType: undefined,
  employmentStatus: undefined,
  politicalStatus: undefined,
  homeAddress: undefined,
  idNumber: undefined,
  leaveDate: undefined,
  leaveReason: undefined,
  hireDate: undefined,
  retirementDate: undefined,
})
const formRules = reactive({
  username: [{ required: true, message: '用户账号不能为空', trigger: 'blur' }],
  password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
  nickname: [{ required: true, message: '用户昵称不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '帐号状态不能为空', trigger: 'change' }],
  leaveDate: [{ required: true, message: '离职时间不能为空', trigger: 'change' }],
  leaveReason: [{ required: true, message: '离职原因不能为空', trigger: 'change' }],
  retirementDate: [{ required: true, message: '退休时间不能为空', trigger: 'change' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await UsersApi.getUsers(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as UsersVO
    if (formType.value === 'create') {
      await UsersApi.createUsers(data)
      message.success(t('common.createSuccess'))
    } else {
      await UsersApi.updateUsers(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    username: undefined,
    password: undefined,
    nickname: undefined,
    remark: undefined,
    deptId: undefined,
    postIds: undefined,
    email: undefined,
    mobile: undefined,
    sex: undefined,
    avatar: undefined,
    status: undefined,
    loginIp: undefined,
    loginDate: undefined,
    schoolCode: undefined,
    birthDate: undefined,
    ethnicity: undefined,
    education: undefined,
    technicalTitle: undefined,
    certificateType: undefined,
    employmentStatus: undefined,
    politicalStatus: undefined,
    homeAddress: undefined,
    idNumber: undefined,
    leaveDate: undefined,
    leaveReason: undefined,
    hireDate: undefined,
    retirementDate: undefined,
  }
  formRef.value?.resetFields()
}
</script>

<style scoped>
:deep(.blue-divider .el-divider__text) {
  color: rgb(64, 158, 255);
}
</style>